#include "PhoneCode.h"

PhoneCode *PhoneCode::globalInstance = new PhoneCode();

PhoneCode::PhoneCode()
{
}

PhoneCode::~PhoneCode()
{
}

void PhoneCode::add(const string &phone, const string &code)
{
    m_mutex.lock(); // 加锁
    unorder_map[phone] = code;
    m_mutex.unlock(); // 解锁
}

void PhoneCode::remove(const string &phone)
{
    m_mutex.lock(); // 加锁
    unorder_map.erase(phone);
    m_mutex.unlock(); // 解锁
}

bool PhoneCode::codeMatch(const string &phone, const string &code)
{
    // 查找手机号是否存在
    if (unorder_map.find(phone) != unorder_map.end())
    {
        // 若存在，判断验证码是否匹配
        string tmp_code = unorder_map[phone];
        if (tmp_code == code)
            return true;
    }
    return false;
}

string PhoneCode::generatorCode()
{
    return string("666666");
}
